package ${package.Controller};

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.*;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@Api(tags = {"${table.comment!}"})
@RestController
<#else>
@Controller
</#if>@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>class ${table.controllerName}<#if superControllerClass??>:${superControllerClass}()</#if><#else><#if superControllerClass??>public class ${table.controllerName} extends ${superControllerClass}{<#else>public class ${table.controllerName} {</#if>

    private Logger log = LoggerFactory.getLogger(getClass());
    @Resource
    private ${table.serviceName} ${(table.serviceName?substring(1))?uncap_first};


    @ApiOperation(value = "新增${table.comment!}")
    @PostMapping()
    public Result add(${entity} ${entity?uncap_first}){
        try {
            return toResult(${(table.serviceName?substring(1))?uncap_first}.add(${entity?uncap_first}));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "异常:" + e.getMessage());
        }
    }

    @ApiOperation(value = "删除${table.comment!}")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Long id){
        try {
            return toResult(${(table.serviceName?substring(1))?uncap_first}.delete(id));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "异常:" + e.getMessage());
        }
    }

    @ApiOperation(value = "更新${table.comment!}")
    @PutMapping()
    public Result update(${entity} ${entity?uncap_first}){
        try {
            return toResult(${(table.serviceName?substring(1))?uncap_first}.updateData(${entity?uncap_first}));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "异常:" + e.getMessage());
        }
    }

    @ApiOperation(value = "查询${table.comment!}分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码"),
        @ApiImplicitParam(name = "pageCount", value = "每页条数")
    })
    @GetMapping()
    public Result findListByPage(@RequestParam Integer page,
                                   @RequestParam Integer pageCount){
        try {
            return Result.success(${(table.serviceName?substring(1))?uncap_first}.findListByPage(page, pageCount));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "异常:" + e.getMessage());
        }
    }

    @ApiOperation(value = "id查询${table.comment!}")
    @GetMapping("{id}")
    public Result findById(@PathVariable Long id){
        try {
            return Result.success(${(table.serviceName?substring(1))?uncap_first}.findById(id));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(500, "异常:" + e.getMessage());
        }
    }

    /**
    * 封装逻辑业务状态
    * @param i 数据库影响结果
    * @return Result
    */
    private Result toResult(int i) {
        if (i > 0) {
            return Result.success("操作成功");
        } else {
            return Result.error(201, "操作失败");
        }
    }

}
</#if>